package kxfang.com.common.dialog;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.umeng.Platform;
import com.hjq.umeng.UmengClient;
import com.hjq.umeng.UmengShare;
import com.zhpan.idea.utils.ToastUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import kxfang.com.common.R;
import kxfang.com.common.databinding.DialogShareBinding;
import kxfang.com.common.databinding.ItemShareBinding;
import kxfang.com.common.views.BaseDialog;


/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/03/23
 *    desc   : 分享对话框
 */
public final class ShareDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder> {

        private final BaseQuickAdapter<ShareBean, BaseViewHolder> mAdapter;

        private final UmengShare.ShareData mData;

        private UmengShare.OnShareListener mListener;

        public Builder(Context context) {
            super(context);

            setContentView(R.layout.dialog_share);

            final List<ShareBean> data = new ArrayList<>();
            data.add(new ShareBean(getDrawable(R.drawable.wechat_fd),"微信", Platform.WECHAT));
            data.add(new ShareBean(getDrawable(R.drawable.wechat_py), "朋友圈", Platform.CIRCLE));
            data.add(new ShareBean(getDrawable(R.drawable.share_qq), "QQ", Platform.QQ));
            data.add(new ShareBean(getDrawable(R.drawable.share_qq_kongj), "QQ空间", Platform.QZONE));
//            data.add(new ShareBean(getDrawable(R.drawable.ic_share_link), getString(R.string.share_platform_link), null));
            mData = new UmengShare.ShareData(context);
            mAdapter = new BaseQuickAdapter<ShareBean, BaseViewHolder>(R.layout.item_share,data) {
                @Override
                protected void onItemViewHolderCreated(@NotNull BaseViewHolder viewHolder, int viewType) {
                    DataBindingUtil.bind(viewHolder.itemView);
                }

                @Override
                protected void convert(@NotNull BaseViewHolder baseViewHolder, @Nullable ShareBean shareBean) {
                    if (shareBean == null){
                        return;
                    }
                    ItemShareBinding binding = baseViewHolder.getBinding();
                    if (binding != null){
                        binding.ivShareImage.setImageDrawable(shareBean.getShareIcon());
                        binding.tvShareText.setText(shareBean.getShareName());
                    }
                }
            };
            mAdapter.setOnItemClickListener(((adapter, view, position) -> {
                Platform platform = mAdapter.getItem(position).getSharePlatform();
                if (platform != null) {
                    UmengClient.share(getActivity(), platform, mData, mListener);
                } else {
                    // 复制到剪贴板
                    getSystemService(ClipboardManager.class).setPrimaryClip(ClipData.newPlainText("url", mData.getShareUrl()));
                    ToastUtils.show("已复制到剪贴板");
                }
                dismiss();
            }));

            RecyclerView recyclerView = findViewById(R.id.rv_share_list);
            recyclerView.setLayoutManager(new GridLayoutManager(context, data.size()));
            recyclerView.setAdapter(mAdapter);
        }

        public Builder setShareTitle(String title) {
            mData.setShareTitle(title);
            return this;
        }

        public Builder setShareDescription(String description) {
            mData.setShareDescription(description);
            return this;
        }

        public Builder setShareLogo(String url) {
            mData.setShareLogo(url);
            return this;
        }

        public Builder setShareLogo(@DrawableRes int id) {
            mData.setShareLogo(id);
            return this;
        }

        public Builder setShareUrl(String url) {
            mData.setShareUrl(url);
            return this;
        }

        public Builder setListener(UmengShare.OnShareListener listener) {
            mListener = listener;
            return this;
        }
    }

    private static class ShareBean {

        /** 分享图标 */
        private final Drawable mShareIcon;
        /** 分享名称 */
        private final String mShareName;
        /** 分享平台 */
        private final Platform mSharePlatform;

        private ShareBean(Drawable icon, String name, Platform platform) {
            mShareIcon = icon;
            mShareName = name;
            mSharePlatform = platform;
        }

        private Drawable getShareIcon() {
            return mShareIcon;
        }

        private String getShareName() {
            return mShareName;
        }

        private Platform getSharePlatform() {
            return mSharePlatform;
        }
    }
}